package handler.login;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import protocol.packet.login.LoginRequest;
import protocol.packet.login.LoginResponse;
import session.Session;
import utils.SessionUtil;

import java.util.Date;
import java.util.UUID;

public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginRequest> {
    @Override
    protected void channelRead0(ChannelHandlerContext  ctx,  LoginRequest request) {
        LoginResponse loginResponse = new LoginResponse();
        String userId = UUID.randomUUID().toString();
        loginResponse.setUserId(userId);
        SessionUtil.bindSession(new Session(userId, request.getUsername()), ctx.channel());
        loginResponse.setSuccess(true);
        //  登录响应
        ctx.channel().writeAndFlush(loginResponse);
    }
    //  用户断线之后取消绑定
    public void channelInactive(ChannelHandlerContext  ctx) {
        SessionUtil.unBindSession(ctx.channel());
    }

}
